{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "from pathlib import Path\n",
    "import json\n",
    "from openai import OpenAI\n",
    "from tqdm import tqdm\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_or_update_token(env_file, token_name, token_value):\n",
    "    # Read the existing .env file\n",
    "    env_file_path = Path(env_file)\n",
    "    if not env_file_path.exists():\n",
    "        print(f\"{env_file} does not exist.\")\n",
    "        # create an .env file\n",
    "        env_file_path.touch()\n",
    "        print(f\".env file created at {env_file_path}\")\n",
    "    \n",
    "    # Read lines from the .env file\n",
    "    with open(env_file, 'r') as file:\n",
    "        print(f\"{env_file} exists.\")\n",
    "        lines = file.readlines()\n",
    "    \n",
    "    # Track if the token was updated\n",
    "    token_exists = False\n",
    "    \n",
    "    # Modify the existing token if it exists\n",
    "    for i, line in enumerate(lines):\n",
    "        if line.startswith(f\"{token_name}=\"):\n",
    "            print(f\"{token_name} already exists.\")\n",
    "            lines[i] = f\"{token_name}={token_value}\\n\"\n",
    "            token_exists = True\n",
    "            break\n",
    "    \n",
    "    # If the token does not exist, append it\n",
    "    if not token_exists:\n",
    "        print(f\"Add {token_name}.\")\n",
    "        lines.append(f\"{token_name}={token_value}\\n\")\n",
    "    \n",
    "    # Write the lines back to the .env file\n",
    "    with open(env_file, 'w') as file:\n",
    "        file.writelines(lines)\n",
    "    \n",
    "    print(f\"Token {token_name} has been {'updated' if token_exists else 'added'} successfully.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# add_or_update_token('../APIS/.env', 'DEEPSEEK_API_KEY', '')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(dotenv_path = '../APIS/.env')\n",
    "DEEPSEEK_API_KEY = os.getenv('DEEPSEEK_API_KEY')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url=\"https://api.deepseek.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('survey_prompt_misinfo.txt', 'r') as f:\n",
    "    survey_prompt = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 300/300 [54:32<00:00, 10.91s/it]\n"
     ]
    }
   ],
   "source": [
    "results = []\n",
    "n = 300\n",
    "for idx in tqdm(range(0, n), total = n):\n",
    "   \n",
    "    response = client.chat.completions.create(\n",
    "        model=\"deepseek-chat\",\n",
    "        # model = 'deepseek-reasoner',\n",
    "        messages=[\n",
    "            {\"role\": \"user\", \"content\": survey_prompt},\n",
    "        ],\n",
    "        stream=False,\n",
    "        max_tokens=250,\n",
    "        temperature=0\n",
    "    )\n",
    "\n",
    "    results.append(response.choices[0].message.content)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "formatted_results = []\n",
    "for res in results:\n",
    "    match = re.search(r'```json\\n(.*)\\n```', res, re.DOTALL)\n",
    "\n",
    "    if match:\n",
    "        json_str = match.group(1)  # Extract matched JSON string\n",
    "        json_dict = json.loads(json_str)  # Convert to dictionary\n",
    "        formatted_results.append(json_dict)  # Output the dictionary\n",
    "    else:\n",
    "        print(\"No JSON found.\")\n",
    "        print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "501"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(formatted_results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "JSON data saved to results/deepseek_misinfo.json\n"
     ]
    }
   ],
   "source": [
    "file_name = 'results/deepseek_misinfo.json'\n",
    "\n",
    "with open(file_name, \"w\", encoding=\"utf-8\") as json_file:\n",
    "    json.dump(formatted_results, json_file, indent=4)\n",
    "\n",
    "print(\"JSON data saved to\", file_name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
